<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <!-- 1. 按照业务要求编写校验规则对象（rules）
      2. el-form组件绑定表单对象（model）和规则对象（rules）
      3. el-form-item组件通过prop属性指定要使用的校验规则 -->
      <div class="title">智慧园区-登录</div>
      <el-form ref="form" :model="loginForm" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="loginForm.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="loginForm.password" show-password />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="loginForm.checked">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 定义全局变量名
const FORMDATA_KEY = 'form_key'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        checked: false
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  // 生命周期函数，在数据挂载时判断是否回显用户信息
  mounted() {
    const cacheFormStr = localStorage.getItem(FORMDATA_KEY)
    if (cacheFormStr) {
      const cacheUser = JSON.parse(cacheFormStr)
      this.loginForm.username = cacheUser.username
      this.loginForm.password = cacheUser.password
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return
        }
        console.log('登录')
        // store实例通过dispatch方法调用Actions调用接口
        // 添加await是为了同步，防止异步情况直接跳转首页
        const { username, password, checked } = this.loginForm
        await this.$store.dispatch('user/loginAction', { username, password })
        this.$router.push('/')

        // 判断是否需要本地存储用户信息
        if (checked) {
          localStorage.setItem(FORMDATA_KEY, JSON.stringify({ username, password }))
        } else {
          // 否则就将用户信息清楚
          localStorage.removeItem(FORMDATA_KEY)
        }
      })
    }
  }
}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
